﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Person;
using ManagementApi.Models.Project;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Base;
using Modules.FaceScan;
using Modules.FaceScan.Entitys;
using Modules.Person;
using Org.BouncyCastle.Utilities.Encoders;
using PmSoft.FileStorage;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 系统管理-人脸识别记录
    /// </summary>
    [Route("/api/v1/sysuser/personFaceScanRecord")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class PersonFaceScanRecordController : ControllerBase
    {
        private readonly FaceScanRecordService faceScanRecordService;
        private readonly IFileStorage fileStorage;
        private readonly PersonService personService;
        private readonly FacePhotoService personFaceService;
        private readonly IMapper mapper;

        public PersonFaceScanRecordController(FaceScanRecordService faceScanRecordService, FacePhotoService personFaceService, IMapper mapper, IFileStorage fileStorage, PersonService personService)
        {
            this.faceScanRecordService = faceScanRecordService;
            this.mapper = mapper;
            this.fileStorage = fileStorage;
            this.personService = personService;
            this.personFaceService = personFaceService;
        }

        /// <summary>
        /// 人脸识别记录分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("personFaceScanRecord_page")]
        public ApiResult<PageDto<PersonFaceScanRecordDto>> GetPersonFaceScanRecords([FromQuery] FaceScanRecordQuery query, int pageIndex = 1, int pageSize = 20)
        {
            var pageDto = faceScanRecordService.GetRecords(query, pageIndex, pageSize);
            var dtos = pageDto.AsPageDto(m => m.AsDto());
            return ApiResult<PageDto<PersonFaceScanRecordDto>>.Success(dtos);
        }

        /// <summary>
        /// 认证图片预览
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("getImage/{id}")]
        public ApiResult<string> GetImage(int id)
        {
            FaceScanRecord? personFaceScanRecord = faceScanRecordService.GetRecord(id);
            if (personFaceScanRecord == null)
                return ApiResult<string>.Failure("人脸识别记录数据不存在");
            var imgBase64 = "";
            if (!string.IsNullOrEmpty(personFaceScanRecord.CameraPhoto))
            {
                byte[] objectData = fileStorage.GetFileAsync(FileBuckets.Person, personFaceScanRecord.CameraPhoto).GetAwaiter().GetResult();
                if (objectData.Length > 0)
                    imgBase64 = "data:image/png;base64," + Base64.ToBase64String(objectData);
            }
            return ApiResult<string>.Success(imgBase64);
        }

        /// <summary>
        /// 身份证照片预览
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("getImageByIdCard/{idCard}")]
        public ApiResult<string> GetImageByIdCard(string idCard)
        {
            FacePhoto? personFace = personFaceService.GetPersonFaceByIdCard(idCard);
            if (personFace == null)
                return ApiResult<string>.Failure("身份证图片记录数据不存在");
            var imgBase64 = "";
            if (!string.IsNullOrEmpty(personFace.IdCardPhoto))
            {
                byte[] objectData = fileStorage.GetFileAsync(FileBuckets.Person, personFace.IdCardPhoto).GetAwaiter().GetResult();
                if (objectData.Length > 0)
                    imgBase64 = "data:image/png;base64," + Base64.ToBase64String(objectData);
            }
            return ApiResult<string>.Success(imgBase64);
        }

        /// <summary>
        /// 核验照片预览
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("getImageByIdCardVerify/{idCard}")]
        public ApiResult<string> GetImageByIdCardVerify(string idCard)
        {
            FacePhoto? personFace = personFaceService.GetPersonFaceByIdCard(idCard);
            if (personFace == null)
                return ApiResult<string>.Failure("身份证图片记录数据不存在");
            var imgBase64 = "";
            if (!string.IsNullOrEmpty(personFace.VerifyPhoto))
            {
                byte[] objectData = fileStorage.GetFileAsync(FileBuckets.Person, personFace.VerifyPhoto).GetAwaiter().GetResult();
                if (objectData.Length > 0)
                    imgBase64 = "data:image/png;base64," + Base64.ToBase64String(objectData);
            }
            return ApiResult<string>.Success(imgBase64);
        }

        /// <summary>
        /// 替换核验照片
        /// </summary>
        /// <param name="personFaceEditForm"></param>
        /// <returns></returns>
        [HttpPut("updatePersonFacePhoto")]
        public ApiResult UpdatePersonFacePhoto(PersonFaceEditForm personFaceEditForm)
        {
            var personFaceScanRecord = faceScanRecordService.GetRecord(personFaceEditForm.PersonFaceScanRecordId);
            if (personFaceScanRecord == null)
                return ApiResult.Failure("识别记录不存在");
            FacePhoto? personFace = personFaceService.GetPersonFaceByIdCard(personFaceEditForm.IdCard);
            if (personFace == null)
                return ApiResult.Failure($"人员{personFaceEditForm.IdCard}信息不存在");
            personFace.VerifyPhoto = personFaceScanRecord.CameraPhoto;
            personFace.VerifyPhotover = personFace.VerifyPhotover == null ? 1 : personFace.VerifyPhotover + 1;
            personFace.VerifyPhotoTime = DateTime.Now;
            personFaceService.Update(personFace);
            return ApiResult.Success("核验照片更新成功");
        }
    }
}